有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

扩展AbstractTestNGSpringContextTests时,java Autowired spring依赖项在抽象基类中始终为空

我有一个抽象的测试基类,它从AbstractTestNGSpringContextTests扩展而来:

@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@ActiveProfiles(resolver = ActiveProfileResolver.class)
public abstract class BaseTest extends AbstractTestNGSpringContextTests {
    @Autowired
    private MyActions myActions;

    @BeforeSuite(dependsOnMethods = "beforeSuite", alwaysRun = true)
    public void beforeUISuite() {
        myActions.doSomething();
    }
}

以及从BaseTest扩展的测试类

public class ExampleTest extends BaseTest {
    @Test
    public void exampleTest( ){
        // Do something
    }
}

beforeUISuite方法运行时,myActions始终为空。为什么这不是由Spring自动连接的?测试运行时ExampleTest中的自动连接依赖项设置正确,但当beforeUISuite执行时,没有任何连接

如果我使myActions受到保护并注释掉beforeUISuite,则在测试运行时myActions被正确实例化。在执行标记为@BeforeSuite的方法之前,它似乎没有连接


共 (0) 个答案